package com.bxd.baodan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.bxd.baodan.R;
import com.bxd.baodan.adapter.CommonAdapter;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.item.CompanyItem;
import com.bxd.baodan.item.CompanyItem.DataBean.ListBean;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.widget.MyGridView;
import com.bxd.baodan.widget.MyListView;

import java.util.ArrayList;
import java.util.List;

public class AllCompanyActivity extends BaseActivity {
    MyListView myListView;
    List<ListBean> list = new ArrayList<>();
    String name;

    CommonAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insure_company);

        setActionBarTitle("全部公司");
        myListView = (MyListView) findViewById(R.id.my_lv);

        name = getIntent().getStringExtra("company_name");


        adapter = new CommonAdapter<ListBean>(this, list, R.layout.company_item) {
            @Override
            public void convert(ViewHolder holder, ListBean item) {
                ((TextView) holder.getView(R.id.company_sort)).setText(item.title);


                ((MyGridView) holder.getView(R.id.company_mgv)).setAdapter(
                        new CommonAdapter<ListBean.CompanyBean>(
                                AllCompanyActivity.this,
                                item.company,
                                R.layout.company_text_item) {
                            @Override
                            public void convert(ViewHolder holder, final ListBean.CompanyBean item) {
                                if (name.equals(item.company_short_name)) {
                                    ((TextView) holder.getView(R.id.company_name)).setPressed(true);
                                } else {
                                    ((TextView) holder.getView(R.id.company_name)).setPressed(false);
                                }

                                ((TextView) holder.getView(R.id.company_name)).setText(item.company_short_name);
                                holder.getView(R.id.company_name).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // showToast(item.company_short_name);
                                        Intent intent = new Intent();
                                        intent.putExtra("company_name", item.company_short_name);
                                        intent.putExtra("company_id", item.company_id);
                                        setResult(200, intent);
                                        AllCompanyActivity.this.finish();
                                    }
                                });
                            }
                        });
            }
        };

        myListView.setAdapter(adapter);


        String cache = SpUtil.getInstance().getString("companylist");
        if (cache != null && !cache.isEmpty()) {
            CompanyItem companyItem = new Gson().fromJson(cache, CompanyItem.class);
            List<ListBean> netList = companyItem.data.list;

            if (netList != null && netList.size() > 0) {

                list.clear();
                list.addAll(netList);
                adapter.notifyDataSetChanged();
            }
        }
        NetUtil netUtil = new NetUtil("product/companylist", this);
        netUtil.postRequest("", new RequestStringListener() {
            @Override
            public void onComplete(String result) {
                if (JSONUtil.isSuccess(result)) {

                    CompanyItem companyItem = new Gson().fromJson(result, CompanyItem.class);
                    List<ListBean> netList = companyItem.data.list;

                    if (netList != null && netList.size() > 0) {
                        SpUtil.getInstance().savaString("companylist", result);
                        list.clear();
                        list.addAll(netList);
                        list.get(0).company.remove(0);
                        adapter.notifyDataSetChanged();
                    }

                }
            }
        });


    }


}
